import 'package:flutter/material.dart';
import 'package:flutter_app/page/setting/component/settingCom.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:image_picker/image_picker.dart';

class SettingPage extends StatefulWidget{

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _settingState();
  }

}
class _settingState extends State{
  var _image;
  Future getImage(int source) async {
    var image;
    try{
      if(source == 1){
        image = await ImagePicker.pickImage(source: ImageSource.camera);

      }else{
        image = await ImagePicker.pickImage(source: ImageSource.gallery);
      }
    }catch(e){
      Fluttertoast.showToast(
          msg: "相机调取失败",
          timeInSecForIos: 1,
          backgroundColor: Colors.white,
          textColor: Colors.black,
          fontSize: 14.0,
        gravity: ToastGravity.CENTER
      );
    }
    setState(() {
      _image = image;
    });
  }
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
        appBar: PreferredSize(child: AppBar(
          title: Text('设置'),
          centerTitle: true,
        ), preferredSize: Size.fromHeight(44)),
        body: ListView(
          children: <Widget>[
            GestureDetector(
              child: Container(
                padding: EdgeInsets.fromLTRB(20, 10  , 20, 10),
                child: SettingCom('修改头像'),
              ),
              onTap: (){
                _bottomPop(context);
              },
            ),
          ],
        )
    );;
  }

  Widget _bottomPop(context){
    showModalBottomSheet(
      context: context,
      builder: (BuildContext context) {
        return new Container(
          width: 10,
          height: 100.0,
          color: Colors.transparent,
          child: Column(
            children: <Widget>[
              MaterialButton(
                color: Colors.lightBlue,
                minWidth: 240,
                textColor: Colors.white,
                child: new Text('照相机'),
                onPressed: (){
                  getImage(1);
                },
              ),
              MaterialButton(
                color: Colors.lightBlue,
                minWidth: 240,
                textColor: Colors.white,
                child: new Text('相册'),
                onPressed: (){
                  getImage(0);
                },
              )
            ],
          ),
        );
      },
    ).then((val) {
      print(val);
    });
  }

}